package com.rrd.dw.mr.gzip.kafka;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.fayanyuan.FayanyuanData;
import com.ucredit.babel.common.model.third.fayanyuan.FayanyuanPersonRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 法研院-自然人失信
 *
 * @author renzhanzheng
 */

public class FaYanYuanDishonestyParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 法研院-自然人失信
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        FayanyuanPersonRecord obj = (FayanyuanPersonRecord)model;
        FayanyuanData data = obj.getData();
        String apiType = data.getApiType();
        if (!("." + apiType).equals(getSubType()) && !("." + apiType).equals(getApiType())) {
            return false;
        }
        JSONObject info = obj.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        JSONObject result = info.getJSONObject("data") == null ? new JSONObject() : info.getJSONObject("data");
        JSONArray sxbzxrArray = result.getJSONArray("sxbzxr") == null ? new JSONArray() : result.getJSONArray("sxbzxr");
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder txSb = getBaseInfo(obj);
        StringBuilder detailSb = new StringBuilder(500);
        int i = 0;
        for (Object o : sxbzxrArray) {
            i++;
            detailSb.delete(0, detailSb.length());
            JSONObject v = (JSONObject)o;
            //序号
            detailSb.append(i).append(CRT001);
            //性别
            detailSb.append(v.getString("xb") == null ? "" : v.getString("xb")).append(CRT001);
            //执行法院
            detailSb.append(v.getString("zxfy") == null ? "" : v.getString("zxfy")).append(CRT001);
            //省份
            detailSb.append(v.getString("sf") == null ? "" : v.getString("sf")).append(CRT001);
            //执行依据文号
            detailSb.append(v.getString("zxyjwh") == null ? "" : v.getString("zxyjwh")).append(CRT001);
            //立案时间（日期）
            detailSb.append(v.getString("larq") == null ? "" : v.getString("larq")).append(CRT001);
            //案号
            detailSb.append(v.getString("ah") == null ? "" : v.getString("ah")).append(CRT001);
            //出执行依据单位
            detailSb.append(v.getString("zxyjdw") == null ? "" : v.getString("zxyjdw")).append(CRT001);
            //生效法律文书确定的义务
            detailSb.append(v.getString("yw") == null ? "" : v.getString("yw")).append(CRT001);
            //被执行人的履行情况
            detailSb.append(v.getString("lxqk") == null ? "" : v.getString("lxqk")).append(CRT001);
            //失信被执行人行为具体情形
            detailSb.append(v.getString("xwqx") == null ? "" : v.getString("xwqx")).append(CRT001);
            //发布时间（日期）
            detailSb.append(v.getString("fbrq") == null ? "" : v.getString("fbrq")).append(CRT001);
            //组织机构代码
            detailSb.append(v.getString("zzjgdm") == null ? "" : v.getString("zzjgdm")).append(CRT001);
            //企业法人
            detailSb.append(v.getString("qyfr") == null ? "" : v.getString("qyfr")).append(CRT001);

            key.set(getKey());
            val.set((sb.toString() + detailSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        if (i == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }
    @Override
    public String getSubType() {
        return ".loseCredit";
    }

    public String getApiType(){
        return ".loseCreditV1";
    }

    @Override
    public String getTableName() {
        return "kafka_babel_fayanyuan_dishonesty_inc";
    }

    @Override
    public String getType() {
        return RecordType.FAYANYUAN_PERSON_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "法研院-自然人失信";
    }
}
